package com.ciwong.epaper.modules.epaper.ui;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.adapter.BookOrServiceAdapter;
import com.ciwong.epaper.modules.epaper.bean.BasePaging;
import com.ciwong.epaper.modules.epaper.bean.NewBook;
import com.ciwong.epaper.modules.epaper.bean.Subject;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.me.bean.Clazz;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.widget.GradeSelectDialog;
import com.ciwong.epaper.widget.ItemSubjectType;
import com.ciwong.epaper.widget.PullToRefreshView;
import com.ciwong.epaper.widget.RecycleViewDivider;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/12/21.
 */

public class AllServiceOrBookActivity extends BaseActivity implements PullToRefreshView.OnHeaderRefreshListener, PullToRefreshView.OnFooterRefreshListener/*OnRefreshListener, OnLoadMoreListener */ {

	//学科进入
	public static final int TYPE_ALL_GOODS_AND_SERVICE = 0;
	//热门推荐
	public static final int TYPE_HOT_RECOMMEND = 2;
	//新书上架
	public static final int TYPE_NEW_BOOK = 3;
	//精品书籍
	public static final int TYPE_HOT_GOOD_BOOKS = 4;
	//优质服务
	public static final int TYPE_HOT_GOOD_SERVICE = 5;

	private int mSourceType = 0;
	public RecyclerView recyclerView;
	private BookOrServiceAdapter mBookOrServiceAdapter;
	//	public SwipeToLoadLayout swipeToLoadLayout;
	private PullToRefreshView pullToRefreshView;

	protected boolean isRefesh = true;
	private int mPageIndex = 1;
	protected boolean isEmpty = false;
	private ItemSubjectType item_subject_type;
	private TextView btn_grade_condition;
	private int mGradeId;
	private int mSubjectId;
	private LinearLayout ll_grade_subject_condition;
	private RelativeLayout rel_search_empty;
	private Subject mSubject;
	private TextView tx_no_data_tips;

	private int salesType;
	private int applyType;
	private int mTotalCount = 0;

	@Override
	protected void preCreate() {
		super.preCreate();
		Intent intent = getIntent();
		if (null != intent) {
			mSourceType = intent.getIntExtra(IntentFlag.INTENT_FLAG_TYPE, TYPE_ALL_GOODS_AND_SERVICE);
			mSubject = (Subject) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ);
		}

	}

	@Override
	protected int setView() {
		return R.layout.activity_all_service_orbook;
	}

	@Override
	protected void findViews() {
		recyclerView = (RecyclerView) findViewById(R.id.swipe_target);
//		swipeToLoadLayout = (SwipeToLoadLayout) findViewById(R.id.swipeToLoadLayout);
		pullToRefreshView = (PullToRefreshView) findViewById(R.id.ptr_service_or_book);
		item_subject_type = (ItemSubjectType) findViewById(R.id.item_subject_type);
		btn_grade_condition = (TextView) findViewById(R.id.btn_grade_condition);
		ll_grade_subject_condition = (LinearLayout) findViewById(R.id.ll_grade_subject_condition);
		rel_search_empty = (RelativeLayout) findViewById(R.id.rel_search_empty);
		tx_no_data_tips = findViewById(R.id.tx_no_data_tips);
	}

	@Override
	protected void initEvent() {

//		swipeToLoadLayout.setOnRefreshListener(this);
//		swipeToLoadLayout.setOnLoadMoreListener(this);
		pullToRefreshView.setOnHeaderRefreshListener(this);
		pullToRefreshView.setOnFooterRefreshListener(this);

		findViewById(R.id.btn_select_grade).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				GradeSelectDialog gradeSelectDialog = new GradeSelectDialog(AllServiceOrBookActivity.this, new GradeSelectDialog.OnGradeSelectListener() {
					@Override
					public void onGradeSelect(int gradeId, String gradeName) {
						btn_grade_condition.setText(gradeName);
						mGradeId = gradeId;
						mSubjectId = 0;
						mPageIndex = 1;
						mTotalCount=0;
						isRefesh = true;
						Clazz clazz = EApplication.getInstance().getClazz();
						int schoolId = 0;
						String areaId = "0";
						if (clazz != null) {
							areaId = clazz.getSchoolArea();
							schoolId = clazz.getSchoolID();
						}
						item_subject_type.setGradeId(gradeId, null, salesType, areaId, schoolId, applyType);

						getListDataFormNet(mPageIndex, mGradeId, mSubjectId);
					}
				});
				gradeSelectDialog.show();
			}
		});

		item_subject_type.setOnSubjectSelectListener(new ItemSubjectType.OnSubjectSelectListener() {
			@Override
			public void onSubjectSelect(int position, Subject subject) {
				mPageIndex = 1;
				mSubjectId = subject.getSubjectId();
				isRefesh = true;
				getListDataFormNet(mPageIndex, mGradeId, mSubjectId);
			}
		});
	}


	@Override
	protected void loadData() {
		getListDataFormNet(mPageIndex, mGradeId, mSubjectId);
	}

	@Override
	protected void init() {
		switch (mSourceType) {
			case TYPE_ALL_GOODS_AND_SERVICE:
				setTitleText("全部商品");
				ll_grade_subject_condition.setVisibility(View.VISIBLE);
				salesType = 2;
				applyType = 0;
				break;
			case TYPE_HOT_RECOMMEND:
				setTitleText("热门推荐");
				ll_grade_subject_condition.setVisibility(View.GONE);
				salesType = 2;
				applyType = 2;
				break;
			case TYPE_NEW_BOOK:
				setTitleText("新书上架");
				ll_grade_subject_condition.setVisibility(View.GONE);
				salesType = 2;
				applyType = 3;
				break;
			case TYPE_HOT_GOOD_BOOKS:
				setTitleText("精品书籍");
				ll_grade_subject_condition.setVisibility(View.VISIBLE);
				salesType = 1;
				applyType = 4;
				break;
			case TYPE_HOT_GOOD_SERVICE:
				setTitleText("优质服务");
				ll_grade_subject_condition.setVisibility(View.VISIBLE);
				tx_no_data_tips.setText("该地区暂时没有相关服务哦");
				salesType = 0;
				applyType = 5;
				break;
		}

		btn_grade_condition.setText(EApplication.getInstance().getGrade().getGradeName());
		mGradeId = EApplication.getInstance().getGrade().getGradeId();

		if (null != mSubject) {
			mSubjectId = mSubject.getSubjectId();
		}
		Clazz clazz = EApplication.getInstance().getClazz();
		int schoolId = 0;
		String areaId = "0";
		if (clazz != null) {
			areaId = clazz.getSchoolArea();
			schoolId = clazz.getSchoolID();
		}
		item_subject_type.setGradeId(mGradeId, mSubject, salesType, areaId, schoolId, applyType);

		mBookOrServiceAdapter = new BookOrServiceAdapter();

		recyclerView.setLayoutManager(new LinearLayoutManager(this));
		recyclerView.setAdapter(mBookOrServiceAdapter);
//		swipeToLoadLayout.setOnRefreshListener(this);
//		swipeToLoadLayout.setOnLoadMoreListener(this);
		pullToRefreshView.setOnHeaderRefreshListener(this);
		pullToRefreshView.setOnFooterRefreshListener(this);
		recyclerView.addItemDecoration(new RecycleViewDivider(AllServiceOrBookActivity.this, LinearLayoutManager.HORIZONTAL, 1, getResources().getColor(R.color.light_gray15)));

	}


//	@Override
//	public void onLoadMore() {
//		isRefesh = false;
//		getListDataFormNet(mPageIndex, mGradeId, mSubjectId);
//	}

//	@Override
//	public void onRefresh() {
//		isRefesh = true;
//		mPageIndex = 1;
//		getListDataFormNet(mPageIndex, mGradeId, mSubjectId);
//	}

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		isRefesh = false;
		getListDataFormNet(mPageIndex, mGradeId, mSubjectId);
	}

	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		isRefesh = true;
		mPageIndex = 1;
		getListDataFormNet(mPageIndex, mGradeId, mSubjectId);
	}

	private void getListDataFormNet(int pageIndex, int gradeId, int subjectId) {
		if (!NetworkUtils.isOnline()) {
			ToastUtil.INSTANCE.toastCenterNoNetError();
			//			swipeToLoadLayout.setRefreshing(false);
//			swipeToLoadLayout.setLoadingMore(false);
			pullToRefreshView.onHeaderRefreshComplete();
			pullToRefreshView.onFooterRefreshComplete();
			showEmptyView();
		} else {
			showCricleProgress();
			Clazz clazz = EApplication.getInstance().getClazz();
			int schoolId = 0;
			String areaId = "0";
			if (clazz != null) {
				areaId = clazz.getSchoolArea();
				schoolId = clazz.getSchoolID();
			}

			switch (mSourceType) {
				case TYPE_ALL_GOODS_AND_SERVICE:
					EpaperDao.getInstance().getBookOrServiceList(EApplication.BRAND_ID, pageIndex, 10, schoolId, gradeId, subjectId, 2, areaId, null, baseExtCallBack);
					break;
				case TYPE_NEW_BOOK:
					EpaperDao.getInstance().getBookOrServiceListByType(EApplication.BRAND_ID, TYPE_NEW_BOOK, pageIndex, 10, schoolId, gradeId, subjectId, areaId, baseExtCallBack);
					break;
				case TYPE_HOT_RECOMMEND:

					EpaperDao.getInstance().getBookOrServiceListByType(EApplication.BRAND_ID, TYPE_HOT_RECOMMEND, pageIndex, 10, schoolId, gradeId, subjectId, areaId, baseExtCallBack);
					break;

				case TYPE_HOT_GOOD_BOOKS:
					EpaperDao.getInstance().getBookOrServiceListByType(EApplication.BRAND_ID, TYPE_HOT_GOOD_BOOKS, pageIndex, 10, schoolId, gradeId, subjectId, areaId, baseExtCallBack);
					break;
				case TYPE_HOT_GOOD_SERVICE:
					EpaperDao.getInstance().getBookOrServiceListByType(EApplication.BRAND_ID, TYPE_HOT_GOOD_SERVICE, pageIndex, 10, schoolId, gradeId, subjectId, areaId, baseExtCallBack);
					break;
			}
		}
	}

	BaseExtCallBack baseExtCallBack = new BaseExtCallBack(AllServiceOrBookActivity.this) {

		@Override
		public void failed(int errorCode, Object data) {
      		super.failed(errorCode, data);
			if (errorCode != 17 & errorCode != 27 & errorCode != 10004) {
				pullToRefreshView.onHeaderRefreshComplete();
				pullToRefreshView.onFooterRefreshComplete();
				ToastUtil.INSTANCE.toastCenterError(R.string.connect_server_error);
				showEmptyView();
			}
			hideCricleProgress();
		}

		@Override
		public void failed(Object data) {
			super.failed(data);
			hideCricleProgress();
			pullToRefreshView.onHeaderRefreshComplete();
			pullToRefreshView.onFooterRefreshComplete();
			ToastUtil.INSTANCE.toastCenterError(R.string.connect_server_error);
			showEmptyView();
		}

		@Override
		public void success(Object data) {
//			swipeToLoadLayout.setVisibility(View.VISIBLE);
//			pullToRefreshView.setVisibility(View.VISIBLE);
			BasePaging<NewBook> basePaging = (BasePaging<NewBook>) data;
			List<NewBook> netList = null;
			if (null != basePaging) {
				netList = basePaging.getPageList();
				mTotalCount = basePaging.getTotalCount();
			}
//			swipeToLoadLayout.setRefreshing(false);
//			swipeToLoadLayout.setLoadingMore(false);
			pullToRefreshView.onHeaderRefreshComplete();
			pullToRefreshView.onFooterRefreshComplete();



			if (isRefesh) {
				//如果只有一页或者没有直接设置为没有更多了


				if ((null != netList && netList.size() > 0) ) {
					isEmpty = false;
//                                SerializableManager.getInstance().serialize(getSerilizeBookListKey(), (Serializable) netList);
					mPageIndex++;

				} else {
					//数目为0
//                                netList = new ArrayList<>();
					isEmpty = true;
				}
				mBookOrServiceAdapter.setDataList(netList);

			} else {
				if (null != netList && netList.size() > 0) {
					isEmpty = false;
					mBookOrServiceAdapter.addDataList(netList);
					mPageIndex++;
				} else {
//					Toast.makeText(AllServiceOrBookActivity.this, "没有更多数据了", Toast.LENGTH_SHORT).show();
					pullToRefreshView.setFooterState(PullToRefreshView.PULL_TO_NO_MORE);
				}
			}

			//总量相等说明数据已经全部加在完毕
			if(mTotalCount==mBookOrServiceAdapter.getDataList().size()){
				pullToRefreshView.setFooterState(PullToRefreshView.PULL_TO_NO_MORE);
			}else{
				pullToRefreshView.setFooterState(PullToRefreshView.PULL_TO_REFRESH);
			}



			if (!isEmpty) {
				showNormalView();
			} else {
				showEmptyView();
			}
//                        handle_no_net_tips.setVisibility(View.GONE);
//                        List<NewBook> list=( List<NewBook>)data;
//                        Log.d(TAG, "######list.size()#######"+list.size());

			hideCricleProgress();
		}
	};


	public void showNormalView() {
//        ll_grade_subject_condition.setVisibility(View.VISIBLE);
		rel_search_empty.setVisibility(View.GONE);
		pullToRefreshView.setVisibility(View.VISIBLE);
	}

	public void showEmptyView() {
//        ll_grade_subject_condition.setVisibility(View.VISIBLE);
		rel_search_empty.setVisibility(View.VISIBLE);
		pullToRefreshView.setVisibility(View.GONE);
	}


}
